// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var approve_upload_css = require("./approve_upload.css");
    var router = require("../hash/pageRouter");
    var flag = false;
    var businessLicenceImage;
    var certificateImage;
    var legalPersonFrontImage;
    var legalPersonBackImage;
    var creditReportImage;
    var qualificationsImage;
    var tools = require("tools");
    var databind = require("databind");
	var imgObj;
	var variable;
	var authenticationId;
	var tools = require("tools");
    var init = function(page){
    	authenticationId = page.params.authenticationId;
    	//禁止通过返回上一页返回本页
    	history.go(1);
    	
    	databind.fillInput1($("#certificates_upload"), {action: "inquireCurrentEnterpriseDetail.json"}, function(data){
    		if ($("#certificates_upload input[name=creditReportImage]").val() != "")
    			$("#creditReportImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid=" + $("#certificates_upload input[name=creditReportImage]").val());
    		if ($("#certificates_upload input[name=qualificationsImage]").val() != "")
    			$("#qualificationsImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid=" + $("#certificates_upload input[name=qualificationsImage]").val());
    		if ($("#certificates_upload input[name=legalPersonBackImage]").val() != "")
    			$("#legalPersonBackImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid=" + $("#certificates_upload input[name=legalPersonBackImage]").val());
    		if ($("#certificates_upload input[name=legalPersonFrontImage]").val() != "")
    			$("#legalPersonFrontImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid=" + $("#certificates_upload input[name=legalPersonFrontImage]").val());
    		if ($("#certificates_upload input[name=certificateImage]").val() != "")
    			$("#certificateImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid=" + $("#certificates_upload input[name=certificateImage]").val());
    		if ($("#certificates_upload input[name=businessLicenceImage]").val() != "")
    			$("#businessLicenceImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid=" + $("#certificates_upload input[name=businessLicenceImage]").val());
    		
    		creditReportImage = $("#certificates_upload input[name=creditReportImage]").val();
    		qualificationsImage = $("#certificates_upload input[name=qualificationsImage]").val();
    		legalPersonBackImage = $("#certificates_upload input[name=legalPersonBackImage]").val();
    		legalPersonFrontImage = $("#certificates_upload input[name=legalPersonFrontImage]").val();
    		certificateImage = $("#certificates_upload input[name=certificateImage]").val();
    		businessLicenceImage = $("#certificates_upload input[name=businessLicenceImage]").val();
		});
		initEvent();
    };
	var initEvent = function(){
		
		$("#approveBack").unbind("click").click(function(){
			location.href = "../approve/update_QualificationAuthentication.html?authenticationId=" + authenticationId;
		});

		//营业执照 businessLicenceImage
		tools.fileupload($("#businessLicenceImage"), function(data, d){
			//上传进度
			var progress = parseInt(data.loaded / data.total * 100, 10);
			var str = "<div id='progress1' class='progress'><div>";
			$(".label1").append(str);
			$("#progress1").radialIndicator({
		        barColor: '#87CEEB',
		        barWidth: 10,
		        initValue: 0,
		        roundCorner : true,
		        percentage: true
		    });
		    var radialObj = $("#progress1").data('radialIndicator');
			//now you can use instance to call different method on the radial progress.
			//like
			radialObj.animate(progress);
		}, function(data){
			//回调函数
			businessLicenceImage = data.result.data[0].fileUuid;
			if (businessLicenceImage != "" && businessLicenceImage != null) {
				$("#businessLicenceImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid="+ data.result.data[0].fileUuid);
			}
			$("#progress1").remove();
		});
		
		//组织机构代码图片 certificateImage
		tools.fileupload($("#certificateImage"), function(data, d){
			//上传进度
			var progress = parseInt(data.loaded / data.total * 100, 10);
			var str = "<div id='progress2' class='progress'><div>";
			$(".label1").append(str);
			$("#progress2").radialIndicator({
		        barColor: '#87CEEB',
		        barWidth: 10,
		        initValue: 0,
		        roundCorner : true,
		        percentage: true
		    });
		    var radialObj = $("#progress2").data('radialIndicator');
			//now you can use instance to call different method on the radial progress.
			//like
			radialObj.animate(progress);
		}, function(data){
			//回调函数
			certificateImage = data.result.data[0].fileUuid;
			if (certificateImage != "" && certificateImage != null) {
				$("#certificateImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid="+ data.result.data[0].fileUuid);
			}
			$("#progress2").remove();
		});
		
		//法人正面照 legalPersonFrontImage
		tools.fileupload($("#legalPersonFrontImage"), function(data, d){
			//上传进度
			var progress = parseInt(data.loaded / data.total * 100, 10);
			var str = "<div id='progress3' class='progress'><div>";
			$(".approve2").append(str);
			$("#progress3").radialIndicator({
		        barColor: '#87CEEB',
		        barWidth: 10,
		        initValue: 0,
		        roundCorner : true,
		        percentage: true
		    });
		    var radialObj = $("#progress3").data('radialIndicator');
			//now you can use instance to call different method on the radial progress.
			//like
			radialObj.animate(progress);
		}, function(data){
			//回调函数
			legalPersonFrontImage = data.result.data[0].fileUuid;
			if (legalPersonFrontImage != "" && legalPersonFrontImage != null) {
				$("#legalPersonFrontImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid="+ data.result.data[0].fileUuid);
			}
			$("#progress3").remove();
		});
		
		//法人代表反面照 legalPersonBackImage
		tools.fileupload($("#legalPersonBackImage"), function(data, d){
			//上传进度
			var progress = parseInt(data.loaded / data.total * 100, 10);
			var str = "<div id='progress4' class='progress'><div>";
			$(".approve2").append(str);
			$("#progress4").radialIndicator({
		        barColor: '#87CEEB',
		        barWidth: 10,
		        initValue: 0,
		        roundCorner : true,
		        percentage: true
		    });
		    var radialObj = $("#progress4").data('radialIndicator');
			//now you can use instance to call different method on the radial progress.
			//like
			radialObj.animate(progress);
		}, function(data){
			//回调函数
			legalPersonBackImage = data.result.data[0].fileUuid;
			if (legalPersonBackImage != "" && legalPersonFrontImage != null) {
				$("#legalPersonBackImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid="+ data.result.data[0].fileUuid);
			}
			$("#progress4").remove();
		});
		
		//公司资质creditReportImage
		tools.fileupload($("#creditReportImage"), function(data, d){
			//上传进度
			var progress = parseInt(data.loaded / data.total * 100, 10);
			var str = "<div id='progress5' class='progress'><div>";
			$(".approve3").append(str);
			$("#progress5").radialIndicator({
		        barColor: '#87CEEB',
		        barWidth: 10,
		        initValue: 0,
		        roundCorner : true,
		        percentage: true
		    });
		    var radialObj = $("#progress5").data('radialIndicator');
			//now you can use instance to call different method on the radial progress.
			//like
			radialObj.animate(progress);
		}, function(data){
			//回调函数
			creditReportImage = data.result.data[0].fileUuid;
			if (creditReportImage != "" && creditReportImage != null) {
				$("#creditReportImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid="+ data.result.data[0].fileUuid);
			}
			$("#progress5").remove();
		});
		
		//信用报告qualificationsImage
		tools.fileupload($("#qualificationsImage"), function(data, d){
			//上传进度
			var progress = parseInt(data.loaded / data.total * 100, 10);
			var str = "<div id='progress6' class='progress'><div>";
			$(".approve3").append(str);
			$("#progress6").radialIndicator({
		        barColor: '#87CEEB',
		        barWidth: 10,
		        initValue: 0,
		        roundCorner : true,
		        percentage: true
		    });
		    var radialObj = $("#progress6").data('radialIndicator');
			//now you can use instance to call different method on the radial progress.
			//like
			radialObj.animate(progress);
		}, function(data){
			//回调函数
			qualificationsImage = data.result.data[0].fileUuid;
			if (qualificationsImage != "" && qualificationsImage != null) {
				$("#qualificationsImage").next().attr("src", "/finance/api/v1.0/downloadFile.file?fileUuid="+ data.result.data[0].fileUuid);
			}
			$("#progress6").remove();
		});
		
//		添加点击事件
		$("#main").on("click", "#approve_update_next", function(){
			if($("#legalPersonBackImage").val()==null){
				$(".error_info").html("请选择法人代表反面照！").css("visibility", "visible");
				flag = false;
			}else if($("#legalPersonFrontImage").val()==null){
				$(".error_info").html("请选择法人代表正面照！").css("visibility", "visible");
				flag = false;
			}else if($("#certificateImage").val()==null){
				$(".error_info").html("请选择组织机构代码图片！").css("visibility", "visible");
				flag = false;
			}else if($("#businessLicenceImage").val()==null){
				$(".error_info").html("请选择营业执照！").css("visibility", "visible");
				flag = false;
			}else{
				flag = true;
			}
			
			if(flag){
				tools.protectDoubleClick();
				supplementEnterpriseImage();
			}
		});
		
    };
	
    //查询企业详细信息
	var inquireEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireCurrentEnterpriseDetail.json",
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0].enterpriseGradeEvaluationDetail != null) {
						databind.fillForm($("#main"), data.datas[0]);
					}
				} else {
					base.alert(2, data.message);
				}
				//判断认证状态
					if(data.datas[0].authentication !=undefined && data.datas[0].authentication != 3) { //未认证
						$(".return").hide();
					}
					if(data.datas[0].authentication !=undefined && data.datas[0].authentication == 3) { //已认证
						$(".next").hide();
					}
				//调显示与隐藏函数
				hideSpan();
		        hideCardType();
			}
		});
		databind.enableInput("#main");
	};
	 //增补企业证照
    var supplementEnterpriseImage = function(){
		base.fnAjax({
			action: "authenticationEnterpriseImage.json",
			async: false,
			oData: {
                creditReportImage: creditReportImage,
                qualificationsImage: qualificationsImage,
                legalPersonBackImage: legalPersonBackImage,
                legalPersonFrontImage: legalPersonFrontImage,
                certificateImage: certificateImage,
                businessLicenceImage: businessLicenceImage,
                authenticationId: authenticationId,
            }, //传递参数
            fnSuccess: function(data) {
            	tools.cancelProtectDoubleClick();
                if (data.status == 200) {
					router.route("approve/certificates_commit.html?authenticationId=" + authenticationId);
                } else {
                	flag = false;
//                  base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
    return {
        init:init
    };
});